using Gtk;
using NHibernate.Cfg;
using NHibernate;
using NHibernate.Transform;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace PNhibernate
{
	public class CategoriaVistaNombre
	{
		public string Nombre {get;set;}
	}
	
	class MainClass
	{
		public static void Main (string[] args)
		{
			Application.Init ();
			
			Configuration configuration = new Configuration();
			configuration.Configure();
			
			configuration.AddAssembly("PNhibernate");
			
			NHibernate.Tool.hbm2ddl.SchemaExport schemaExport = new NHibernate.Tool.hbm2ddl.SchemaExport(configuration);
			
			schemaExport.SetOutputFile("dbprueba.hbm2ddl.sql");
			schemaExport.Execute(false, false, false);
			
			//1 vez
			ISessionFactory sessionFactory = configuration.BuildSessionFactory();

			//n veces
			ISession session = sessionFactory.OpenSession();
			
			IQuery query = session.CreateQuery("from Categoria");
			
			IList<Categoria> categorias = query.List<Categoria>();
			
			foreach (Categoria categoria in categorias)
				Console.WriteLine("Id={0} Nombre={1}", categoria.Id, categoria.Nombre);
			
			//n veces
			session.Close();

			//n veces
			session = sessionFactory.OpenSession();
			
			query = session.CreateQuery("select Nombre as Nombre from Categoria");
			
			foreach (NHibernate.Type.IType nHType in query.ReturnTypes)
				Console.WriteLine("nHType.Name={0}", nHType.Name);

			foreach (string alias in query.ReturnAliases)
				Console.WriteLine("alias={0}", alias);
			
			query.SetResultTransformer(Transformers.AliasToBean<CategoriaVistaNombre>());
			
			IList<CategoriaVistaNombre> vistaCategorias = query.List<CategoriaVistaNombre>();
			
			foreach (CategoriaVistaNombre categoriaVistaNombre in vistaCategorias)
				Console.WriteLine("Nombre={0}", categoriaVistaNombre.Nombre);
			
			//n veces
			session.Close();
			
			//1 vez
			sessionFactory.Close();
			
			MainWindow win = new MainWindow ();
			win.Show ();
			Application.Run ();
		}
	}
}
